package tool

import (
	"io/ioutil"

	paillier "github.com/roasbeef/go-go-gadget-paillier"
)

// 加密文件内容
func encryptFile(inputFile, outputFile string, pubKey *paillier.PublicKey) error {
	data, err := ioutil.ReadFile(inputFile)
	if err != nil {
		return err
	}

	ciphertext, err := paillier.Encrypt(pubKey, data)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(outputFile, ciphertext, 0644)
}

// 解密文件内容
func decryptFile(inputFile, outputFile string, privKey *paillier.PrivateKey) error {
	ciphertext, err := ioutil.ReadFile(inputFile)
	if err != nil {
		return err
	}

	plaintext, err := paillier.Decrypt(privKey, ciphertext)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(outputFile, plaintext, 0644)
}
